using Microsoft.EntityFrameworkCore;
using WinBookKeeping.Models;

namespace WinBookKeeping.Data
{
    public class BookKeepingContext : DbContext
    {
        public DbSet<IncomeInfo> Incomes { get; set; }
        public DbSet<ExpendInfo> Expenditures { get; set; }
        public DbSet<RecordInfo> Items { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 设置数据库文件路径为应用程序目录下的 BookKeeping.db
            string dbPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BookKeeping.db");
            optionsBuilder.UseSqlite($"Data Source={dbPath}");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置收入表
            modelBuilder.Entity<IncomeInfo>(entity =>
            {
                entity.HasKey(e => e.IncomeId);
                entity.Property(e => e.ItemName).IsRequired();
                entity.Property(e => e.Money).HasColumnType("decimal(18,2)");
                entity.Property(e => e.IncomeTime).IsRequired();
            });

            // 配置支出表
            modelBuilder.Entity<ExpendInfo>(entity =>
            {
                entity.HasKey(e => e.expendId);
                entity.Property(e => e.ItemName).IsRequired();
                entity.Property(e => e.Money).HasColumnType("decimal(18,2)");
                entity.Property(e => e.expendTime).IsRequired();
            });

            // 配置名目表
            modelBuilder.Entity<RecordInfo>(entity =>
            {
                entity.HasKey(i => i.Id);
                entity.Property(i => i.ItemName).IsRequired();
                entity.Property(i => i.ItemType).IsRequired();
            });
        }
    }
} 